<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\api;

use app\common\controller\ApiController;
use think\App;
use app\mill\model\Stat as StatModel;
use app\mill\model\Order as OrderModel;
use app\mill\model\Archives as ArchivesModel;
use app\mill\model\Give as GiveModel;
use app\mill\model\Client as ClientModel;
use app\admin\model\Config as ConfigModel;
use think\Db;

/**
 * 小程序公开控制器
 * @package app\user\admin
 */
class Xcx extends ApiController
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);

        $this->is_member();
    }

    /**
     * 工程阶段统计
     */
    public function statOrderStatus()
    {
        //默认今天
        $starttime = strtotime(date("Y-m-d", (time())));
        $endtime = strtotime(date("Y-m-d 23:59:59", time()));

        $time_input = input('time');

        $data = StatModel::statOrderStatus($starttime,$endtime,$time_input);

        success('',$data);
    }

    /**
     * 首页统计
     */
    public function statIndex(){


        //----------------------------------------- 时间 -----------------------------------------

        //本日
        $day_date = strtotime(date('Y-m-d'));
        $day_end_date = strtotime('+1 day',$day_date) - 1;
        //昨日
        $upday_end_date = $day_date- 1;
        $upday_date = strtotime(date('Y-m-d',$upday_end_date));
        //本月
        $month_date = mktime(0,0,0,date('m'),1,date('Y'));
        $month_end_date = mktime(23,59,59,date('m'),date('t'),date('Y'));
        //上月
        $upmonth_end_date = $month_date - 1;
        $upmonth_date = strtotime(date('Y-m-1',$upmonth_end_date));
        //本年
        $year_date = mktime(0,0,0,1,1,date('Y'));
        $year_end_date = strtotime('+1 year',$year_date) - 1;
        //去年
        $upyear_end_date = $year_date - 1;
        $upyear_date = strtotime(date('Y-1-1',$upyear_end_date));

        //----------------------------------------- 客户 -----------------------------------------

        $client['count'] = Db::name('mill_client')->count();
        //今日
        $client['day_count'] = Db::name('mill_client')->where([['create_time','between time',[$day_date,$day_end_date]]])->count();
        //昨日
        $client['upday_count'] = Db::name('mill_client')->where([['create_time','between time',[$upday_date,$upday_end_date]]])->count();
        //本月
        $client['month_count'] = Db::name('mill_client')->where([['create_time','between time',[$month_date,$month_end_date]]])->count();
        //上月
        $client['upmonth_count'] = Db::name('mill_client')->where([['create_time','between time',[$upmonth_date,$upmonth_end_date]]])->count();
        //本年
        $client['year_count'] = Db::name('mill_client')->where([['create_time','between time',[$year_date,$year_end_date]]])->count();
        //去年
        $client['upyear_count'] = Db::name('mill_client')->where([['create_time','between time',[$upyear_date,$upyear_end_date]]])->count();
        //日同比
        $client['day_same'] = StatModel::doSame($client['day_count'],$client['upday_count']);
        //月同比
        $client['month_same'] = StatModel::doSame($client['upmonth_count'],$client['upmonth_count']);
        //年同比
        $client['year_same'] = StatModel::doSame($client['year_count'],$client['upyear_count']);

        //----------------------------------------- 工程 -----------------------------------------

        $order['count'] = Db::name('mill_order')->count();
        //今日
        $order['day_count'] = Db::name('mill_order')->where([['create_time','between time',[$day_date,$day_end_date]]])->count();
        //昨日
        $order['upday_count'] = Db::name('mill_order')->where([['create_time','between time',[$upday_date,$upday_end_date]]])->count();
        //本月
        $order['month_count'] = Db::name('mill_order')->where([['create_time','between time',[$month_date,$month_end_date]]])->count();
        //上月
        $order['upmonth_count'] = Db::name('mill_order')->where([['create_time','between time',[$upmonth_date,$upmonth_end_date]]])->count();
        //本年
        $order['year_count'] = Db::name('mill_order')->where([['create_time','between time',[$year_date,$year_end_date]]])->count();
        //去年
        $order['upyear_count'] = Db::name('mill_order')->where([['create_time','between time',[$upyear_date,$upyear_end_date]]])->count();
        //日同比
        $order['day_same'] = StatModel::doSame($order['day_count'],$order['upday_count']);
        //月同比
        $order['month_same'] = StatModel::doSame($order['upmonth_count'],$order['upmonth_count']);
        //年同比
        $order['year_same'] = StatModel::doSame($order['year_count'],$order['upyear_count']);

        //----------------------------------------- 生产 -----------------------------------------

        //本月初 至 昨天
        $stat_order_s = StatModel::statOrderStatus($month_date,$upday_end_date);
        //上月
        $upstat_order_s = StatModel::statOrderStatus($upmonth_date,$upmonth_end_date);
        //同比
        $stat_order_s_same = [
            //'bill'          =>  StatModel::doSame($stat_order_s['bill'],$upstat_order_s['bill']),
            'status1'       =>  StatModel::doSame($stat_order_s['status1']['area_sum'],$upstat_order_s['status1']['area_sum']),
            'status2'       =>  StatModel::doSame($stat_order_s['status2']['area_sum'],$upstat_order_s['status2']['area_sum']),
            'status3'       =>  StatModel::doSame($stat_order_s['status3']['area_sum'],$upstat_order_s['status3']['area_sum']),
            'status4'       =>  StatModel::doSame($stat_order_s['status4']['area_sum'],$upstat_order_s['status4']['area_sum']),
        ];

        //--------------------------------------- 生产周期 ---------------------------------------

        $order_pro['min'] = Db::name('mill_order')->where('give_pro > 0')->order('give_pro asc')->value('give_pro');
        $order_pro['max'] = Db::name('mill_order')->where('give_pro > 0')->order('give_pro DESC')->value('give_pro');
        $order_pro['avg'] = round($order_pro['min']+$order_pro['max'],2)/2;

        //----------------------------------------- 设计 -----------------------------------------

        //本月件数，面积
        $design = Db::name('mill_order')
            ->alias('o')
            ->field('sum((a.one_area*a.number)) as area_sum,sum(a.number) as number_sum')
            ->leftJoin('mill_order_archives a','o.id=a.order_id')
            ->where([['o.archives_bill_time','between time',[$month_date,$month_end_date]]])
            ->find();
        $design['area_sum'] = floatval($design['area_sum']);
        $design['number_sum'] = floatval($design['number_sum']);
        //本月下单
        $design['order'] = $order['month_count'];
        //较上月面积同比
        $design['area_sum_same'] = StatModel::doSame($design['area_sum'],$upstat_order_s['bill']['area_sum']);
        //本月转化率
        $design['conver'] = Db::name('mill_order')
            ->alias('o')
            ->leftJoin('mill_order_archives_give g','o.id=g.order_id')
            ->where([['o.archives_bill_time','between time',[$month_date,$month_end_date]],['g.status','>',0]])
            ->count();
        $design['conver'] = empty($design['order'])?100:round($design['conver']/$design['order'],2)*100;

        //--------------------------------------- 生产载荷 ---------------------------------------

        //待生产
        $load['status0'] = Db::name('mill_order_archives_give')->alias('g')->leftJoin('mill_order_archives a','a.id=g.archives_id')->where('g.status=0')->sum('a.one_area');
        //在生产
        $load['status1'] = Db::name('mill_order_archives_give')->alias('g')->leftJoin('mill_order_archives a','a.id=g.archives_id')->where('g.status in(1,2)')->sum('a.one_area');
        //未完成总量
        $load['status2'] = $load['status0']+$load['status1'];
        //负载率
        $load['rate'] = round($load['status0']/$load['status2'],2)*100;

        success('',['client'=>$client,'order'=>$order,'stat_order_s'=>$stat_order_s,'upstat_order_s'=>$upstat_order_s,'stat_order_s_same'=>$stat_order_s_same,'order_pro'=>$order_pro,'design'=>$design,'load'=>$load]);
    }

    /**
     * 客户列表
     */
    public function clientList(){
        //$user = $this->member;
        //$role_status = GiveModel::$role[$user['group']];

        $search = trim(input('search'));//关键词搜索
        $pindex = max(1, intval(input('page',1)));
        $psize = 20;

        $where = [];
        $order = 'c.id desc';

        if($search){
            $where = "(c.nickname LIKE '%{$search}%' OR c.mobile LIKE '%{$search}%' OR c.back LIKE '%{$search}%')";
        }

        $order = trim($order,',');
        $list = Db::name('mill_client')
            ->alias('c')
            ->field("c.*,sum((a.one_area*a.number)) as area_sum,sum(a.number) as number_sum,count(DISTINCT o.id) as order_sum")
            ->leftJoin('mill_order o','c.id=o.client_id')
            ->leftJoin('mill_order_archives a','o.id=a.order_id')
            ->where($where)
            ->group('c.id')
            ->order($order)
            ->limit((($pindex-1)*$psize),$psize)
            ->select();

        $total = Db::name('mill_client')
            ->alias('c')
            ->where($where)
            ->count();

        success('', array('list' => $list, 'pagesize' => $psize, 'total' => $total, 'pindex' => $pindex, 'total_pages' => max(1,ceil($total/$psize))));
    }

    /**
     * 工程列表
     */
    public function orderList(){
        //$user = $this->member;
        //$role_status = GiveModel::$role[$user['group']];

        $search = trim(input('search'));//关键词搜索
        //$order_id = input('order_id');//工程ID
        $client_id = input('client_id');//客户ID
        $time_input = input('time');
        $pindex = max(1, intval(input('page',1)));
        $psize = 20;

        $where = '1 ';
        $order = 'o.id desc';

        if($client_id)$where .= " AND o.client_id={$client_id} ";
        if($search){
            $where .= " AND (o.archives_sn LIKE '%{$search}%' OR o.engineering LIKE '%{$search}%' OR c.company LIKE '%{$search}%') ";
        }

        if(!empty($time_input)){
            $time_input = explode(' - ',$time_input);
            $starttime = strtotime($time_input[0]);
            $endtime = strtotime(date("Y-m-d 23:59:59", strtotime($time_input[1])));

            $where .= " AND o.archives_sign_time BETWEEN {$starttime} AND {$endtime} ";
        }

        $order = trim($order,',');
        $list = Db::name('mill_order')
            ->alias('o')
            ->field("o.*,count(DISTINCT oo.id) as order_sum")
            ->leftJoin('mill_client c','c.id=o.client_id')
            ->leftJoin('mill_order oo','oo.order_pid=o.order_pid')
            ->where($where)
            ->group('o.order_pid')
            ->order($order)
            ->limit((($pindex-1)*$psize),$psize)
            ->select();

        if(!empty($list)){
            foreach ($list as &$v){

                if(empty($v['contract']))$v['contract'] = '无';
                if(!empty($v['archives_sign_time']))$v['archives_sign_time'] = date('Y-m-d',$v['archives_sign_time']);
                $v['status_str'] = OrderModel::$status[$v['status']];
                $v['status_verify'] = Db::name('admin_user')
                    ->alias('u')
                    ->where("u.status=1 and u.id={$v['status_verify_uid']}")
                    ->leftJoin('admin_user_group g','g.id=u.group')
                    ->value("concat(g.name,'[',u.nickname,']')");
                if(empty($v['status_verify']))$v['status_verify'] = '无';

                $order_archives = Db::name('mill_order_archives')
                    ->field('sum(row_area) as area_sum,sum(number) as number_sum')
                    ->where(['order_pid'=>$v['order_pid']])
                    ->find();
                $v['area_sum'] = isset($order_archives['area_sum'])?$order_archives['area_sum']:'';
                $v['number_sum'] = isset($order_archives['number_sum'])?$order_archives['number_sum']:'';

            }
        }

        $total = Db::name('mill_order')
            ->alias('o')
            ->field("DISTINCT o.order_pid")
            ->leftJoin('mill_client c','c.id=o.client_id')
            ->where($where)
            ->count();

        success('', array('list' => $list, 'pagesize' => $psize, 'total' => $total, 'pindex' => $pindex, 'total_pages' => max(1,ceil($total/$psize))));
    }

    /**
     * 批次列表
     */
    public function orderPList(){
        //$user = $this->member;
        //$role_status = GiveModel::$role[$user['group']];

        $search = trim(input('search'));//关键词搜索
        $order_pid = input('order_pid');//工程ID
        $pindex = max(1, intval(input('page',1)));
        $psize = 20;

        $where = '1 ';
        $order = 'o.id asc';

        if($order_pid)$where .= " AND o.order_pid={$order_pid} ";
        if($search){
            $where .= " AND (o.archives_sn LIKE '%{$search}%' OR o.engineering LIKE '%{$search}%' OR c.company LIKE '%{$search}%') ";
        }

        $order = trim($order,',');
        $list = Db::name('mill_order')
            ->alias('o')
            ->where($where)
            ->order($order)
            ->limit((($pindex-1)*$psize),$psize)
            ->select();

        if(!empty($list)){
            foreach ($list as &$v){

                $archvices = ArchivesModel::getOneStat($v['id'],'order_id');
                $v = array_merge($v,$archvices);

                $v['status_time_str'] = '无';
                if(!empty($v['status_time']))$v['status_time_str'] = date('Y-m-d',$v['status_time']);
                $v['status_time_str'] = '无';
                if(!empty($v['archives_sign_time']))$v['archives_sign_time_str'] = date('Y-m-d',$v['archives_sign_time']);

            }
        }

        $total = Db::name('mill_order')
            ->alias('o')
            ->field("DISTINCT o.order_pid")
            ->leftJoin('mill_client c','c.id=o.client_id')
            ->where($where)
            ->count();

        //批次统计
        $order_ids = Db::name('mill_order')->alias('o')->where($where)->column('o.id','id');
        $stat = ArchivesModel::getOneStat(implode(',',$order_ids),'order_id','in');

        success('', array('stat'=>$stat,'list' => $list, 'pagesize' => $psize, 'total' => $total, 'pindex' => $pindex, 'total_pages' => max(1,ceil($total/$psize))));
    }

    /**
     * 生产线进度统计
     */
    public function speed()
    {

        $search = trim(input('search'));//关键词搜索
        $archives_status = input('status',1);
        $time_input = input('time');

        // 获取查询条件
        $map = 'g.status > 0 ';

        if($search){
            $map .= " AND (o.archives_sn LIKE '%{$search}%' OR o.engineering LIKE '%{$search}%') ";
        }

        //默认日期：今天
        $starttime = strtotime(date('Y-m-d'));
        $endtime = strtotime('+1 day',$starttime) - 1;

        if(!empty($time_input)){
            $time_input = explode(' - ',$time_input);
            $starttime = strtotime($time_input[0]);
            $endtime = strtotime(date("Y-m-d 23:59:59", strtotime($time_input[1])));
        }

        //生产状态：0待生产，1开料，2打磨，3焊接焊接，4发货
        $map .= " AND g.status_flow like '%_{$archives_status}|%' ";
        $map .= " AND (g.status{$archives_status}_time between {$starttime} AND {$endtime})";

        $field = "o.*,g.status as give_status2,FROM_UNIXTIME(g.status{$archives_status}_time,'%Y-%m-%d') as dates,count(g.id) as day_sum,sum(a.one_area) as area_day_sum";

        // 数据列表
        $list = Db::name('mill_order_archives_give')
            ->alias('g')
            ->field($field)
            ->leftJoin('mill_order o','o.id=g.order_id')
            ->leftJoin('mill_order_archives a','a.id=g.archives_id')
            ->where($map)
            ->group('dates,g.order_id')
            ->order('dates asc,g.order_pid asc')
            ->select();

        $psize = $total = 0;
        if(!empty($list)){
            foreach ($list as &$v){

                //总数
                $where = ['order_id'=>$v['id']];
                $stat = Db::name('mill_order_archives')->field('sum(number) as number_sum,sum((number*one_area)) as area_sum')->where($where)->find();

                $v['number_sum'] = $stat['number_sum'];
                $v['area_sum'] = round($stat['area_sum'],5);

                //完成数
                $where = [['g.order_id','=',$v['id']],['g.quality_status','>=',0],['g.status_flow','like',"%_{$archives_status}|%"]];
                $stat_ok = Db::name('mill_order_archives_give')->alias('g')->field('count(g.id) as number_sum,sum(a.one_area) as area_sum')->leftJoin('mill_order_archives a','a.id=g.archives_id')->where($where)->find();

                $v['number_ok_sum'] = $stat_ok['number_sum'];
                $v['area_ok_sum'] = round($stat_ok['area_sum'],6);

                //未完成数
                $v['number_no_sum'] = $v['number_sum']-$v['number_ok_sum'];
                $v['area_no_sum'] = $v['area_sum']-$v['area_ok_sum'];
                $v['area_no_sum'] = round($v['area_no_sum'],6);

                $v['create_time'] = date('Y-m-d',$v['create_time']);
                $total++;
            }
        }

        success('', array('list' => $list, 'pagesize' => $psize, 'total' => $total, 'pindex' => 1, 'total_pages' => 1));

    }

    /**
     * 发货列表页
     */
    public function deliver(){

        $search = trim(input('search'));//关键词搜索
        $client_id = input('client_id');
        $pindex = max(1, intval(input('page',1)));
        $psize = 20;

        $map = " g.status=5 ";

        if($search){
            //$map .= " AND (c.plate_number LIKE '%{$search}%' OR cl.company LIKE '%{$search}%') OR o.archives_sn LIKE '%{$search}%') OR o.engineering LIKE '%{$search}%') ";
            $map .= " AND (g.archives_sn LIKE '%{$search}%') ";
        }
        //if($client_id)$map .= " AND o.client_id={$client_id} ";

        // 数据列表
        $list = Db::name('mill_order_archives_give')
            ->alias('g')
            ->field("'' as show_i,group_concat(DISTINCT g.order_id) as order_ids,g.car_id,g.status5_time,g.address_time,g.address_date,g.address_sn,g.address_status")
            ->where($map)
            ->where("g.status=5 and g.address_status in (1,2)")
            ->group("g.address_sn")
            ->order('g.address_sn desc')
            ->limit((($pindex-1)*$psize),$psize)
            ->select();

        if(!empty($list)){
            foreach ($list as &$v){
                $order = Db::name('mill_order')
                    ->alias('o')
                    ->field('o.*,group_concat(DISTINCT o.order_sn) as order_sn,group_concat(DISTINCT o.batch) as batch,sum(a.number) as number_sum,sum(a.row_area) as area_sum')
                    ->leftJoin('mill_order_archives a','a.order_id=o.id')
                    ->where([['o.id','in',$v['order_ids']]])
                    ->find();
                $v['order_sn'] = $order['order_sn'];
                $v['batch'] = $order['batch'];
                $v['archivees_sn'] = "{$order['order_sn']}[{$order['batch']}]";
                $v['client_id2'] = $order['client_id'];
                $v['plate_number'] = Db::name('mill_car')->where([['id','=',$v['car_id']]])->value('plate_number');

                $v = array_merge($order,$v);

                //总数
                $v['area_sum'] = round($v['area_sum'],4);
                //已扫
                $give_where = [['g.address_sn','=',$v['address_sn']]];
                $ok = Db::name('mill_order_archives_give')
                    ->alias('g')
                    ->field('count(g.id) as number,sum(a.one_area) as row_area')
                    ->leftJoin('mill_order_archives a','a.id=g.archives_id')
                    ->where($give_where)
                    ->find();
                $v['number_ok_sum'] = $ok['number'];
                $v['area_ok_sum'] = $ok['row_area'];

                //未扫
                $give_where = [['g.order_id','in',$v['order_ids']],['g.status','<',5]];
                $no = Db::name('mill_order_archives_give')
                    ->alias('g')
                    ->field('count(g.id) as number,sum(a.one_area) as row_area')
                    ->leftJoin('mill_order_archives a','a.id=g.archives_id')
                    ->where($give_where)
                    ->find();
                $v['number_no_sum'] = $no['number'];
                $v['area_no_sum'] = round($no['row_area'],4);

                //获取发货地址
                $v['address'] = Db::name('mill_order_archives_address')->where(['address_sn'=>$v['address_sn']])->find();

                //定时任务，超过24小时未确认发货，则自动发货
                $task = GiveModel::taskSubGiveAddress($v['address_sn'],time());
                if($task){
                    $v['address_status'] = $task['address_status'];
                    $v['address_date'] = $task['address_date'];
                    $v['address_time'] = $task['address_time'];
                }

                $v['time_h'] = '0';
                if(!empty($v['address_time'])){

                    $v['time_h'] = round(($v['address_time']-$v['status5_time'])/3600,1);
                    $v['address_time'] = date('Y-m-d H:i:s',$v['address_time']);
                }
                $v['status5_time'] = date('Y-m-d H:i:s',$v['status5_time']);

                //状态 0:装车中 1:未打单 2:已打单
                $status_arr = ['装车中','未打单','已打单'];
                $v['address_status_str'] = $status_arr[$v['address_status']];

            };

        }

        $total = Db::name('mill_order_archives_give')
            ->alias('g')
            ->leftJoin('mill_order o','g.order_id=o.id')
            ->leftJoin('mill_car c','g.car_id=c.id')
            ->leftJoin('mill_client cl','o.client_id=cl.id')
            ->where($map)
            ->group("IF(g.address_sn='',CONCAT(FROM_UNIXTIME(status5_time,'%Y%m%d'),'_|_',order_id),address_sn)")
            ->count();

        success('', array('list' => $list, 'pagesize' => $psize, 'total' => $total, 'pindex' => $pindex, 'total_pages' => max(1,ceil($total/$psize))));

        /*// 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle($title) // 页面标题
            ->setTableName('mill_order') // 设置表名
            ->setSearchArea([
                ['text:2', 'plate_number', '车牌号码', 'like'],
                ['select:3', 'client_id', '客户公司','','',ClientModel::getList('company')],
                ['text:2', 'o.archives_sn', '档案号', 'like'],
                ['text:2', 'o.engineering', '工程名称', 'like'],
            ])
            ->hideCheckbox()
            ->addColumns([ // 批量添加列
                ['plate_number', '车牌号码'],
                ['status4_time', '开始装车时间','datetime'],
                ['address_time', '装车完成时间','datetime'],
                ['address_status','状态','callback',function($value){
                    $status_arr = ['0'=>'未装车完成','1'=>'已装车完成','2'=>'已装车完成'];
                    $status = OrderModel::status_tag($value,$status_arr[$value]);

                    return $status;
                },'__data__'],
                ['client_id2','客户公司','','',ClientModel::getList('company')],
                ['archives_sn', '档案号'],
                ['engineering', '工程名称'],
                ['batch', '批次'],
                //['give_sum','总数量'],
                //['give_sum4','已发货'],
                //['give_sum0_4','未发货'],
                ['right_button', '操作', 'btn']
            ])
            ->fetch(); // 渲染模板*/
    }

    /**
     * 产品明细发货进度
     */
    public function deliverGive()
    {

        $a_sort = input('a_sort');//序号：sort-counter
        $order_ids = input('order_id');//工程ID 未发货
        $address_sn = input('address_sn');//发货单号 已发货
        $pindex = max(1, intval(input('page',1)));
        $psize = 20;
        // 获取查询条件
        $map = [];

        if($order_ids){
            $map[] = ['g.order_id','in',$order_ids];
            $map[] = ['g.status','<',4];
        }
        else $map[] = ['g.address_sn','=',$address_sn];

        //序号搜索 a_sort="{sort}-{counter}"
        if(!empty($a_sort))$a_sort = explode('-',$a_sort);
        $sort = !empty($a_sort[0])?$a_sort[0]:'';
        $counter = !empty($a_sort[1])?$a_sort[1]:'';
        if(!empty($sort))$map[] = ['a.sort','=',$sort];
        if(!empty($counter))$map[] = ['g.counter','=',$counter];

        // 数据列表
        $list = Db::name('mill_order_archives_give')
            ->alias('g')
            ->field("g.*,o.batch as batch,a.model,a.thickness,a.one_area,a.color,a.sort as a_sort,o.engineering")
            ->leftJoin('mill_order_archives a','a.id=g.archives_id')
            ->leftJoin('mill_order o','o.id=g.order_id')
            ->where($map)
            ->order('g.id asc')
            ->limit((($pindex-1)*$psize),$psize)
            ->select();

        if(!empty($list)){
            foreach ($list as &$v){

                $v['a_sort'] = "{$v['sort']}-{$v['counter']}";
                $v['status4_time'] = date('Y-m-d',$v['status4_time']);

                $v['status_str'] = GiveModel::$give_status[$v['status']];
                if($v['quality_status']<0)$v['status_str'] .= GiveModel::$quality_status[$v['quality_status']];

            }
        }

        //统计
        $stat = Db::name('mill_order')
            ->alias('o')
            ->field('sum(g.number) as number_sum,sum(a.one_area) as area_sum')
            ->leftJoin('mill_order_archives a','o.id=a.order_id')
            ->leftJoin('mill_order_archives_give g','a.id=g.archives_id')
            ->where($map)
            ->find();

        $total = Db::name('mill_order_archives_give')
            ->alias('g')
            ->where($map)
            ->count();

        success('', array('stat' => $stat, 'list' => $list, 'pagesize' => $psize, 'total' => $total, 'pindex' => $pindex, 'total_pages' => max(1,ceil($total/$psize))));

    }

    /**
     * 公司列表
     */
    public function getCompany(){

        $list = Db::name('mill_client')->field('id,company')->where('status=1')->select();

        success('', array('list' => $list));
    }

}
